<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="core.*,util.*" %>
<%@ page import="java.util.*" %>
<%@ include file="/WEB-INF/include/checkAdmin.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/WEB-INF/include/header.jsp" %>
<style>
    form > div:not(.box) { margin-top: 20px; }
    div.box label { display: inline-block; margin-right: 20px; }
    body table.table { width: 400px; }
    body table.table tr:first-child { background-color: #eee; }
</style>
</head>
<%
    String errMsg = null,infoMsg = null;

    RequestParameter param = new RequestParameter(request);
    String cmd = param.getString("cmd", "");
    int id = param.getInt("id", 0);
    User user = UserDAO.selectById(id);

    String loginId = param.getString("loginID", user.getLoginId());
    String name = param.getString("name", user.getName());
    String email = param.getString("email", user.getEmail());
    boolean enabled = param.getBoolean("enabled", user.isEnabled());

    if (request.getMethod().equals("POST")) {
        if (id > 0) {
            if ("정보저장".equals(cmd)) {
                if (loginId.length() >= 5) {
                    if (loginId.equals(user.getLoginId()) || UserDAO.selectByLoginId(loginId) == null) {
                        if (name.length() >= 2) {
                            UserDAO.updateInfo(id, loginId, name, email, enabled);
                            infoMsg = "저장되었습니다";
                        }
                        else errMsg = "이름이 너무 짧습니다";
                    }
                    else errMsg = "로그인 아이디가 중복됩니다";
                }
                else errMsg = "로그인 아이디가 너무 짧습니다";
            }
            if ("비밀번호저장".equals(cmd)) {
                String password1 = param.getString("password1", "");
                String password2 = param.getString("password2", "");
                if (password1.length() >= 5) {
                    if (password1.equals(password2)) {
                        UserDAO.updatePasswd(id, UserService.encryptPasswd(password1));
                        infoMsg = "저장되었습니다";
                    }
                    else errMsg = "비밀번호가 일치하지 않습니다";
                }
                else errMsg = "비밀번호가 너무 짧습니다";
            }
            if ("소속그룹저장".equals(cmd)) {
                UserGroupDAO.saveUserGroup(id, param.getIntValues("userGroup"));
                infoMsg = "저장되었습니다";
            }
        }
        else errMsg = "잘못된 접근입니다";
    }

    ArrayList<Integer> thisUserGroup = UserGroupDAO.selectByUserId(id);

    String queryString = request.getQueryString().replaceAll("id=[0-9]+&?","");
    String url = "userList.jsp" + queryString;
%>
<body>
<%@ include file="/WEB-INF/include/menu.jsp" %>

<div class="container main">

    <h1>사용자 정보 수정</h1>
    <hr />

    <form method="post">
        <input type="hidden" name="id" value="<%= id %>" />

        <div>아이디</div>
        <input type="text" name="loginID" value="<%= loginId %>" />

        <div>이름</div>
        <input type="text" name="name" value="<%= name %>" />

        <div>이메일</div>
        <input type="text" name="email" value="<%= email %>" />

        <div>활성화</div>
        <div class="box">
            <label>
                <input type="radio" name="enabled" value="True"  <%= enabled ? "checked" : "" %> />
                True
            </label>
            <label>
                <input type="radio" name="enabled" value="False" <%= enabled ? "" : "checked" %> />
                False
            </label>
        </div>

        <div>
            <button type="submit" class="btn" name="cmd" value="정보저장">
                <i class="icon-hdd"></i> 정보 저장
            </button>
        </div>

        <hr />

        <div>새 비밀번호</div>
        <input type="password" name="password1" />

        <div>새 비밀번호 확인</div>
        <input type="password" name="password2" />

        <div>
            <button type="submit" class="btn" name="cmd" value="비밀번호저장">
                <i class="icon-hdd"></i> 비밀번호 저장
            </button>
        </div>

        <hr />

        <table class="table table-bordered table-condensed">
            <tr><td>사용자 소속 그룹</td><td>범주</td></tr>
            <% for (UserGroup group : UserGroupDAO.selectAll()) { %>
                <tr>
                    <td>
                        <label>
                            <input type="checkbox" name="userGroup" value="<%=group.getId()%>"
                                   <%=thisUserGroup.contains(group.getId()) ? "checked" : ""%> />
                            <%= group.getName() %>
                        </label>
                    </td>
                    <td><%= group.getCategory() %></td>
                </tr>
            <% } %>
        </table>

        <div>
            <button type="submit" class="btn" name="cmd" value="소속그룹저장">
                <i class="icon-hdd"></i> 사용자 소속 그룹 저장
            </button>
            <a href="<%= url %>" class="btn"><i class="icon-list"></i> 목록으로</a>
        </div>

    </form>

    <% if (errMsg != null) { %>
        <div class="alert alert-error"><%= errMsg %></div>
    <% } %>
    <% if (infoMsg != null) { %>
        <div class="alert alert-info"><%= infoMsg %></div>
    <% } %>

</div>

<%@ include file="/WEB-INF/include/footer.jsp" %>
</body>
</html>